#!/bin/bash

sudo omreport storage vdisk controller=0 |grep -i ssd &> /dev/null
if [[ $? -eq 0 ]]
  then
    mkdir -p /tmp/zabbix_ssd_monitor
    sudo omreport storage vdisk controller=0 |egrep 'Name|Device Name|Media'|grep -B2 -i ssd |grep -v -i ssd |sed 's/.*Name.*: //g' |sed 's/ //g'|xargs -l2 |sed 's/\/dev\///g' > /tmp/zabbix_ssd_monitor/device_list
    sudo megacli -cfgdsply –aALL |egrep '^Name|^Slot Number' |sed 's/ //g' |sed 's/:/ /g' > /tmp/zabbix_ssd_monitor/slot_list
    while read name device; do sed -i "s/${name}/${device}/g" /tmp/zabbix_ssd_monitor/slot_list ; done < /tmp/zabbix_ssd_monitor/device_list
    while read name device; do if [[ $name == 'Name' ]]; then device_new="$device"; else echo $name $device |sed "s/^/${device_new} /"; fi; done < /tmp/zabbix_ssd_monitor/slot_list > /tmp/zabbix_ssd_monitor/slot_list.last
    for device in `awk '{print $NF}' |xargs`; do grep $device /tmp/zabbix_ssd_monitor/slot_list.last |awk '{print $1,$NF}' >> /tmp/zabbix_ssd_monitor/ssd_list; done < /tmp/zabbix_ssd_monitor/device_list

    length=`wc -l /tmp/zabbix_ssd_monitor/ssd_list |awk '{print $1}'`
    printf "{\n"
    printf '\t'"\"data\":["
    while read ssd_name ssd_slot
    do
        printf '\n\t\t{'
        printf "\"{#SSD_NAME}\":\"${ssd_name}\",\"{#SSD_SLOT}\":\"${ssd_slot}\"}"
        length=$(($length-1))
        if [ $length -gt 0 ]
        then
            printf ','
        fi
    done < /tmp/zabbix_ssd_monitor/ssd_list
    printf "\n\t]\n"
    printf "}\n"
fi

#smartctl -H -d megaraid,3 /dev/sdc
#smartctl -A -d megaraid,3 /dev/sdc
#Wear_Leveling_Count
#Runtime_Bad_Block

rm -rf /tmp/zabbix_ssd_monitor